package com.capinfo.accounting.web;

import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.capinfo.accumulation.model.accounting.mybist.GB_JZPZXX;
import com.capinfo.accumulation.model.accounting.mybist.HZJZPZ;
import com.capinfo.accumulation.model.pagemodel.Json;
import com.capinfo.accumulation.parameter.accounting.DataGrid;
import com.capinfo.accumulation.service.accounting.mybits.DICTIONARY_PZLBServiceI;
import com.capinfo.accumulation.service.accounting.mybits.GB_JZPZXXServiceI;
import com.capinfo.accumulation.service.accounting.mybits.ZJYZPZGLServiceI;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;

/**
 *  @author zxl
 */
@Controller
@RequestMapping("/business/accounting/zJYZPZGL")
public class ZJYZPZGLController extends BaseController {
	@Autowired
	public ZJYZPZGLServiceI zJYZPZGLService;
	@Autowired
	public GB_JZPZXXServiceI gB_JZPZXXService;
	@Autowired
	DICTIONARY_PZLBServiceI dICTIONARY_PZLBServiceI;
	//定义一个对象用来存储数据
	public static HZJZPZ hZJZPZ;
	//定义一个变量，来确定是否为新增操作
	boolean b;
	
	
	
	@RequestMapping("/dataGrid.shtml")
	@ResponseBody								//b 用来判断是否是第一次加载数据
	public DataGrid dataGrid(@RequestParam  Map param,String b) {
		System.out.println(b);
		if(b != null && b != "") {
			return zJYZPZGLService.findbyByGrid(param);
		}else {
			return null;
		}
		
	}
	
	/**
	 * 
	 * 保存和修改数据
	 */
	@RequestMapping("/add.shtml")
	@ResponseBody
	public Json add(String jsonString,Model model)  throws IllegalAccessException, InvocationTargetException,JsonSyntaxException{
		Json j = new Json();
		
		Gson gson = new Gson(); 
		hZJZPZ = gson.fromJson(jsonString, HZJZPZ.class);	
	//对获取的数据进行判断，如果数据有Id说明是已经存在的数据，修改即可，否则就进行添加操作
		try {	
			if(hZJZPZ.getID() == null) {
				b=false;
				//获取系统当前时间
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
				String time = df.format(new Date());
				//根据日期获取凭证号
				String xSPZH = zJYZPZGLService.getXSPZHByDate(time);
				//将凭证号+1存入对象
				hZJZPZ.setXSPZH(String.valueOf(Integer.parseInt(xSPZH)+1));
				hZJZPZ.setPZRQ(df.parse(time));
				//将新增的资金运作凭证信息存入数据库
				zJYZPZGLService.insertSelective(hZJZPZ);
			}else{
				//修改的操作方法
				b=true;
				zJYZPZGLService.updateByPrimaryKeySelective(hZJZPZ);
			}
			j.setSuccess(true);
			j.setMsg("保存成功");
		} catch (Exception e) {
			 e.printStackTrace();
			 j.setMsg("保存失败");
		}
		return j;
	}
	
	/*
	 * 添加和修改凭证分录信息
	 */
	@RequestMapping("/edit.shtml")
	@ResponseBody
	public Json edit(String jsonString) throws IllegalAccessException, InvocationTargetException{
		Gson gson = new Gson(); 
		Json j = new Json();
		try {			
		  List<GB_JZPZXX> gbList = gson.fromJson(jsonString, new TypeToken<List<GB_JZPZXX>>(){}.getType());  
			  
			  for (GB_JZPZXX gb : gbList) {  
		            if(gb.getID()!=null) {
		            	//说明是修改操作
		            	gB_JZPZXXService.updateByPrimaryKeySelective(gb);
		            }else{
		            	//如果是添加就将信息存入对象中
		            	gb.setJZPZH(hZJZPZ.getXSPZH());
		            	gb.setZTDM(hZJZPZ.getZTDM());
		            	gb.setHSDW(hZJZPZ.getHSDW());
		            	//gb.setPZZTBS(Long.parseLong(hZJZPZ.getPZZTBS()));
		            	gb.setZZBS(hZJZPZ.getZZBJ());
		            	gb.setJZPZH(hZJZPZ.getJZPZH());
		            	gb.setFJDJS(hZJZPZ.getFJDJS());
		            	gb.setJZRQ(hZJZPZ.getJZRQ());
		            	gb.setYWLX(hZJZPZ.getYWLX());
		            	gb.setZJYWLX(hZJZPZ.getZJYWLX());
		            	//gb.setZD(hZJZPZ.getZD());
		            	//gb.setZDRQ(hZJZPZ.getZDRQ());
		            	//gb.setFH(hZJZPZ.getFH());
		            	//gb.setFHRQ(hZJZPZ.getFHRQ());
		            	//gb.setPZZT(hZJZPZ.getPZZT());
		            	//gb.setHZPZH(hZJZPZ.getHZPZH());
		            	gb.setXSPZH(hZJZPZ.getXSPZH());
		            	gb.setPZLB(hZJZPZ.getPZLB());
		            	//gb.setWTSM(hZJZPZ.getWTSM());
		            	gb.setJZ(hZJZPZ.getJZ());
		            	//获取系统的当前时间
		            	gb.setPZRQ(new Date());
		            	gb.setHZPZID(hZJZPZ.getID());
		            	//将数据插入数据库
			        	gB_JZPZXXService.insert(gb);
		            }	
		        }  			
			j.setSuccess(true);
			j.setMsg("保存成功");
			
		} catch (Exception e) {
			 e.printStackTrace();
			j.setMsg("保存失败");
		}
		return j;
	}
	
	/*
	 * 删除资金运作凭证管理的信息
	 */
	@RequestMapping("/delete.shtml")
	@ResponseBody
	public Json delete(String id) {
		Json j = new Json();
		try {
			zJYZPZGLService.deleteByPrimaryKey(id);
			//根据关联的字段同时删除 JZPZXX表中的数据
			zJYZPZGLService.deleteByHZPZId(id);
			j.setSuccess(true);
			j.setMsg("删除成功!");
		} catch (Exception e) {
			 e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		return j;
	}

	@RequestMapping("/selectByid.shtml")
	@ResponseBody
	public HZJZPZ selectByid(String id) {
		return zJYZPZGLService.selectByPrimaryKey(id);
	}
	
	
	/**
	 * 获取页面的下拉框中的信息
	 * @return
	 */
	@RequestMapping("/manager.shtml")
	public String manager(Model model) {
		List<Map<String,Object>> lMap = zJYZPZGLService.hsdw();
		model.addAttribute("hsdw", lMap);
		List<Map<String,Object>> cMap = zJYZPZGLService.cwzt();
		model.addAttribute("cwzt", cMap);
		return "/business/accounting/zJYZPZGL/zJYZPZGL";
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/addPage.shtml")
	public String addPage(HttpServletRequest request,String jsonData) {
		//根据核算单位的ID获取核算单位名称  
		String hsdw = zJYZPZGLService.getHSDWById(hZJZPZ.getHSDW().toString());
		request.setAttribute("hsdw", hsdw);
		return "/business/accounting/zJYZPZGL/zJYZPZGLAdd";
	}
	
	/**
	 * 根据数据的ID获取，分录信息
	 * @param param
	 * @return
	 */
	@RequestMapping("/flGrid.shtml")
	@ResponseBody
	public DataGrid flGrid(@RequestParam  Map param) {
		//获取GB_JZPZXX 表中对应的数据
		if(b) {
			System.out.println("=================================-=-="+hZJZPZ.getID());
			param.put("HZPZID", hZJZPZ.getID());
			return gB_JZPZXXService.findbyByGrid(param);
		}
		else {
			return null;
		}
	} 
	
	
	/**
	 * 获取核算单位的下拉列表
	 */
	@RequestMapping("/getHSDW.shtml")
	@ResponseBody
	public List<Map<String,Object>> getHSDW() {
		List<Map<String,Object>> lMap = zJYZPZGLService.hsdw();
		return lMap;
	}
	/**
	 * 获取财务账套的下拉列表
	 */
	@RequestMapping("/getCWZT.shtml")
	@ResponseBody
	public List<Map<String,Object>> getCWZT() {

		return zJYZPZGLService.cwzt();
	}
	
	/**
	 * 获取凭证类别的下拉
	 */
	
	@RequestMapping("/getPZLB.shtml")
	@ResponseBody
	public List<Map<String,Object>> getPZLB() {
		return dICTIONARY_PZLBServiceI.pzlb();
	}
	/**
	 * 获取资金业务类型
	 * 
	 */
	@RequestMapping("/getZJYWLX.shtml")
	@ResponseBody
	public List<Map<String,Object>> getZJYWLX() {
		return zJYZPZGLService.getZJYWLX();
	}
	/**
	 * 
	 */
	/*@RequestMapping("/getSTR.shtml")
	@ResponseBody
	public void getSTR(String title) {
		getYWLXInfo(title);
		System.out.println("===============================又回来了");
	}*/
	
	@RequestMapping("/getYWLX.shtml")
	@ResponseBody
	public List<Map<String,Object>> getYWLXInfo(String title) {
			System.out.println(zJYZPZGLService.getYWLX(title));
			return zJYZPZGLService.getYWLX(title);

	}
		
	
	
	
	
	/*
	 * 删除分录信息
	 */
	@RequestMapping("/deleteFL.shtml")
	@ResponseBody
	public Json deleteFL(String id) {
		Json j = new Json();
		try {
			gB_JZPZXXService.deleteByPrimaryKey(id);
			j.setSuccess(true);
			j.setMsg("删除成功!");
		} catch (Exception e) {
			 e.printStackTrace();
			j.setMsg("删除失败!");
		}
		return j;
	}
	
	
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/editPage.shtml")
	public String editPage(String id ,HttpServletRequest request) {
		HZJZPZ zJYZPZGL = this.zJYZPZGLService.selectByPrimaryKey(id);
		request.setAttribute("zJYZPZGL", zJYZPZGL);
		return "/zJYZPZGL/zJYZPZGLEdit";
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/viewPage.shtml")
	public String viewPage(String id ,HttpServletRequest request) {
		HZJZPZ zJYZPZGL = this.zJYZPZGLService.selectByPrimaryKey(id);
		request.setAttribute("zJYZPZGL", zJYZPZGL);
		return "/zJYZPZGL/zJYZPZGLView";
	}
}
